/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.core.model;

import hu.glassy.model.common.AbstractEntity;
import java.util.Date;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.util.Assert;

/**
 * Used to activate the user's account.
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
@Entity
@Table(name = "activationtoken")
public class ActivationToken extends AbstractEntity
{

  private static final long serialVersionUID = -4458668310538781711L;

  private static final int TOKEN_LENGTH = 24;
  private static final Boolean DEFAULT_ACTIVE = Boolean.TRUE;

  @Column(name = "token", length = TOKEN_LENGTH, nullable = false, unique = false)
  private String token = UUID.randomUUID().toString().substring(3, TOKEN_LENGTH + 3);

  @Column(name = "active", nullable = false, unique = false)
  private Boolean active = DEFAULT_ACTIVE;

  /**
   * Az a dátum, amikor a token ki lett küldve
   */
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "datesent", nullable = false, unique = false)
  private Date dateSent = new Date();

  /**
   * Az a dátum, amikor a token fel volt használva
   */
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "dateused", nullable = true, unique = false)
  private Date dateUsed;

  @ManyToOne
  @JoinColumn(name = "account_id", referencedColumnName = "id", nullable = false, unique = false)
  private Account account;

  /**
   * Konstruktor
   */
  public ActivationToken()
  {
  }

  /**
   * Konstruktor
   *
   * @param account
   */
  public ActivationToken(final Account account)
  {
    setAccount(account);
  }

  public String getToken()
  {
    return token;
  }

  public void setToken(final String token)
  {
    this.token = token;
  }

  public Boolean isActive()
  {
    return active;
  }

  public void setActive(final Boolean active)
  {
    this.active = active;
  }

  public Date getDateSent()
  {
    return dateSent;
  }

  public void setDateSent(final Date dateSent)
  {
    this.dateSent = dateSent;
  }

  public Date getDateUsed()
  {
    return dateUsed;
  }

  public void setDateUsed(final Date dateUsed)
  {
    this.dateUsed = dateUsed;
  }

  public Account getAccount()
  {
    return account;
  }

  public final void setAccount(final Account account)
  {
    this.account = account;
  }

  public void useToken()
  {
    Assert.isTrue(active, "Inactive token cannot be used.");

    setActive(Boolean.FALSE);
    setDateUsed(new Date());
  }
}
